// pages/course_detail/course_in/course_in.js
const { getAllExtensions } = require('../../../wxParse/showdown.js');
var WxParse = require('../../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 0,
    listArr:[ "课程详情","课程列表","名师介绍","课堂交流","课程评价"],
    isActiveArr:[true,false,false,false,false],
    lastIndex:0,
    course:{},
    lessonlist:[],
    isLike:true,
    memberId:null,
    isBuy:false,
    price:0,
    discount:null,
    title:null,
    courseId:null
  },
  

  //点击购买时间
  buy(){
    const jiage=this.data.price*this.data.discount
    console.log(jiage)
    console.log(this.data.courseId)
    console.log(this.data.title)
    if(!this.data.memberId){
      wx.showToast({
        title: '请登录后再操作！',
        icon:"error"
      })
    }else{
      if(this.data.isBuy){
        wx.showToast({
          title: '该课程已购买',
          icon:"error"
        })
      }else{
        wx.request({
          url: `http://localhost:8085/coursepurchase/one?memberId=${this.data.memberId}&courseId=${this.data.courseId}&purchasePrice=${jiage}`,
          method:"POST",
          success:(res)=>{
            console.log(res.data)
          },
          fail:(err)=>console.log(err)
        })
        wx.request({
          url: "http://localhost:8085/lessonlist?courseId="+this.data.courseId,
          method:"PUT",
          success:(res)=>{
            console.log(res.data)
            this.getlesson(this.data.courseId)
            this.setData({
              isBuy:true
            })
            this.fetchCourseDetail(this.data.title)
            wx.showToast({
              title: '购买成功',
              icon:"success"})
          },
          fail:(err)=>console.log(err)
        })
      }
    }
  }, 


  //点击收藏事件
  addlike(){
    console.log(this.data.memberId)
    if(!this.data.memberId){
      wx.showToast({
        title: '请登录后再操作！',
        icon:'error'
      })
    }else{
      if(this.data.isLike){
        wx.request({
          url: 'http://localhost:8085/coursefavorite/one?memberId='+this.data.memberId+'&courseId='+this.data.courseId,
          method:"POST",
          success:(res)=>{
            console.log(res.data)
            this.fetchCourseDetail(this.data.title)
          },
          fail:(err)=>console.log(err)
        })
      }else{
        wx.request({
          url: 'http://localhost:8085/coursefavorite/delone?memberId='+this.data.memberId+'&courseId='+this.data.courseId,
          method:"DELETE",
          success:(res)=>{
            console.log(res.data)
            this.fetchCourseDetail(this.data.title)
          },
          fail:(err)=>console.log(err)
        })
      }
      this.setData({
        isLike:!this.data.isLike
      })
      console.log(this.data.isLike)
    }
    console.log(this.data.title)
    
  } ,

  // 点击返回上一级
  enterBack(){
    wx.navigateBack({
      delta: 1
    })

  },
    //点击显示对应的组件
    showComponent(e){
      let {index}=e.currentTarget.dataset
      let newArr=[...this.data.isActiveArr]
      newArr[this.data.lastIndex]=false
      newArr[index]=true
      this.setData({
        isActiveArr:newArr,
        lastIndex:index
      })
      console.log(this.data.isActiveArr)
    },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      title : options.title,
      courseId : options.courseId,
      memberId:wx.getStorageSync('memberId')
    })
    
    this.fetchCourseDetail(this.data.title);
    this.getlesson(this.data.courseId);
   this.getisLike(this.data.memberId,this.data.courseId)
   this.getisBuy(this.data.memberId,this.data.courseId)
  },

  // 获取是否购买isBuy
  getisBuy(memberId,courseId){
    if(!memberId){
      this.setData({
        isBuy:true
      })
    }else{
      wx.request({
        url: `http://localhost:8085/coursepurchase/find?memberId=${memberId}&courseId=${courseId}`,
        method:"GET",
        success:(res)=>{
          console.log(res.data)
          if(!res.data.data){
            this.setData({
              isBuy:false
            })
          }else{
            this.setData({
              isBuy:true
            })
          }
          console.log(this.data.isBuy)
        }
      })
    }
  },


  // 获取isLike
  getisLike:function(memberId,courseId){
    if(!memberId){
      this.setData({
        isLike:true
      })
    }else{
      wx.request({
        url: `http://localhost:8085/coursefavorite/find?memberId=${memberId}&courseId=${courseId}`,
        method:"GET",
        success:(res)=>{
          console.log(res.data.data)
          if(!res.data.data){
            this.setData({
              isLike:true
            })
          }else{
            this.setData({
              isLike:false
            })
          }
          console.log(this.data.isLike)
        }
      })
    }

  },

  // 根据课程名查询课程信息
  fetchCourseDetail: function(title) {
    const that = this;
    wx.request({
      url: `http://localhost:8085/course?courseName=${title}&pageNum=1&pageSize=10`, 
      success: (res)=>{
        console.log(res.data);
        if (res.data.code === 200) {
          that.setData({
            course:res.data.data.list[0],
            price:res.data.data.list[0].originalPrice,
            discount:res.data.data.list[0].discount
          });
          console.log(that.data.price)
          console.log(that.data.discount)
        } else {
          wx.showToast({
            title: '获取详情失败',
            icon: 'none'
          });
        }
      },
      fail: function() {
        wx.showToast({
          title: '请求失败',
          icon: 'none'
        });
      }
    });
  },

  // 根据课程id获取该课程的列表
  getlesson: function(courseId){

    console.log(courseId)
    const that = this;
    wx.request({
      url: 'http://localhost:8085/lessonlist?courseId='+courseId,
      method:"GET",
      success:(res)=>{
        console.log(res.data)
        that.setData({
          lessonlist:res.data.data
        })
      },
      fail:(err)=>{
        console.log("请求失败")
        console.log
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})